home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFtoSpr - Star Fighter 3000 graphics converter
- * Savebox for SF3000 Planets file
- * Copyright (C) 2000 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
-
- /* RISC OS library files */
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "saveas.h"
- #include "window.h"
- #include "gadgets.h"
- #include "flex.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "hourglass.h"
- #include "ViewsMenu.h"
- #include "Macros.h"
- #include "TboxBugs.h"
- #include "FilePerc.h"
-
- /* Local headers */
- #include "SFformats.h"
- #include "Utils.h"
- #include "Loader.h"
- #include "Main.h"
- #include "SavePlanets.h"
-
- typedef struct _SavePlanetsData
- {
- ObjectId saveas_id;
- ObjectId window_id;
- char *reset_file_name;
- SF_PlanetsSetHdr *planets_data; /* flex block */
- } SavePlanetsData;
-
- /* -----------------------------------------------------------------------
- * Function prototypes
- */
-
- static ToolboxEventHandler _SavePlanets_savehandler, _SavePlanets_buttonshandler, _SavePlanets_deletedhandler;
-
- /* -----------------------------------------------------------------------
- * Public functions
- */
-
- ObjectId SavePlanets_create(char *savepath, int data_saved, SF_PlanetsSetHdr **planets_data)
- {
- /* Must either RE-ANCHOR planets_data or flex_free() it
- (will only be flex_free'd if we return NULL_ObjectId) */
- SavePlanetsData *newblk;
- char *views_path;
-
- /* Grab memory */
- newblk = malloc(sizeof(SavePlanetsData));
- if(newblk == NULL)
- MG_RETV("NoMem", NULL_ObjectId) /* failure */
- newblk->reset_file_name = NULL;
-
- /* Create object */
- if(E(toolbox_create_object(0, "SprToPla", &newblk->saveas_id)))
- goto errexit_free;
- if(E(saveas_get_window_id(0, newblk->saveas_id, &newblk->window_id)))
- goto errexit_delete;
-
- /* Store default settings */
- if((newblk->reset_file_name = copystring(savepath)) == NULL)
- goto errexit_delete;
-
- /* Add entry to iconbar menu */
- if(data_saved)
- views_path = savepath; /* we have real filepath */
- else
- views_path = ""; /* dummy filepath */
- if(E(ViewsMenu_add(newblk->saveas_id, msgs_lookup_sub1("SprPlaList", tail(savepath, 3)), views_path)))
- goto errexit_delete;
-
- /* Set up window */
- if(E(saveas_set_file_name(0, newblk->saveas_id, savepath))
- || E(saveas_set_file_type(0, newblk->saveas_id, FILETYPE_PLANETS))
- || E(saveas_set_file_size(0, newblk->saveas_id, flex_size((flex_ptr)planets_data))))
- goto errexit_menuremove;
-
- /*
- Note the ObjectDeleted handler is registered AFTER anything that could cause an error and therefore premature deletion!
- */
- if(E(event_register_toolbox_handler(newblk->saveas_id, SaveAs_SaveToFile, _SavePlanets_savehandler, newblk))
- || E(event_register_toolbox_handler(newblk->saveas_id, SaveAs_DialogueCompleted, delete_object_handler, newblk))
- || E(event_register_toolbox_handler(newblk->window_id, ActionButton_Selected, _SavePlanets_buttonshandler, newblk))
- || E(event_register_toolbox_handler(newblk->saveas_id, Toolbox_ObjectDeleted, _SavePlanets_deletedhandler, newblk)))
- goto errexit_menuremove;
-
- flex_reanchor((flex_ptr)&newblk->planets_data, (flex_ptr)planets_data);
- return newblk->saveas_id; /* success */
-
- errexit_menuremove:
- RE(ViewsMenu_remove(newblk->saveas_id))
- errexit_delete:
- RE(toolbox_delete_object(0, newblk->saveas_id))
- errexit_free:
- free(newblk->reset_file_name);
- free(newblk);
- return NULL_ObjectId; /* failure */
- }
-
- /* -----------------------------------------------------------------------
- * Private functions
- */
-
- /*
- * Toolbox event handlers
- */
-
- static int _SavePlanets_savehandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- SaveAsSaveToFileEvent *save_to_file_block = (SaveAsSaveToFileEvent *)event;
- SavePlanetsData *blk = (SavePlanetsData *)handle;
-
- /* Save to compressed Fednet file */
- _kernel_oserror *err = perc_operation(FILEPERC_OP_COMP, save_to_file_block->filename, FILETYPE_PLANETS, (flex_ptr)&blk->planets_data);
- if(err != NULL)
- err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
-
- saveas_file_save_completed(err == NULL, id_block->self_id, save_to_file_block->filename);
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _SavePlanets_buttonshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* ActionButton clicked on underlying window */
- ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
- SavePlanetsData *blk = (SavePlanetsData *)handle;
-
- if(!FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
- return 0; /* not interested */
-
- switch(id_block->self_component) {
- case 0x82bc02: /* Cancel button */
- /* Reset dbox state */
- if(blk->reset_file_name != NULL)
- RE(saveas_set_file_name(0, blk->saveas_id, blk->reset_file_name))
- break;
-
- #ifndef SAVEAS_CRAP /* no point saving state if dbox closes anyway */
- case 0x82bc03: /* Save button */
- /* Record dbox state */
- int len;
- E_RETV(saveas_get_file_name(0, blk->saveas_id, NULL, 0, &len), 1)
- free(blk->reset_file_name);
- blk->reset_file_name = malloc(len+1);
- if(blk->reset_file_name == NULL)
- err_complain(255, msgs_global("NoMem"));
- else
- RE(saveas_get_file_name(0, blk->saveas_id, blk->reset_file_name, len, NULL))
- break;
- #endif
-
- default:
- return 0; /* unknown component */
- }
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _SavePlanets_deletedhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- SavePlanetsData *blk = (SavePlanetsData *)handle;
-
- /* Deregister handlers */
- RE(event_deregister_toolbox_handler(id_block->self_id, SaveAs_SaveToFile, _SavePlanets_savehandler, handle))
- RE(event_deregister_toolbox_handler(id_block->self_id, SaveAs_DialogueCompleted, delete_object_handler, handle))
- RE(event_deregister_toolbox_handler(blk->window_id, ActionButton_Selected, _SavePlanets_buttonshandler, handle))
- RE(event_deregister_toolbox_handler(id_block->self_id, Toolbox_ObjectDeleted, _SavePlanets_deletedhandler, handle))
-
- /* Remove from iconbar menu */
- RE(ViewsMenu_remove(id_block->self_id))
-
- /* free memory */
- flex_free((flex_ptr)&blk->planets_data);
- free(blk->reset_file_name);
- free(blk);
-
- if(last_savebox == id_block->self_id)
- last_savebox = NULL_ObjectId;
-
- return 1; /* claim event */
- }
-